package com.worklog.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.worklog.mapper.RoleMapper;
import com.worklog.mapper.RoleMenuMapper;
import com.worklog.mapper.RoleProductMapper;
import com.worklog.mapper.RoleResourcesMapper;
import com.worklog.model.Role;
import com.worklog.model.RoleMenu;
import com.worklog.model.RoleProduct;
import com.worklog.model.RoleResources;
import com.worklog.model.UserRole;
import com.worklog.service.RoleService;

import tk.mybatis.mapper.entity.Example;

@Service("roleService")
public class RoleServiceImpl extends BaseService<Role> implements RoleService {

	@Resource
	private RoleMapper roleMapper;
	@Resource
	private RoleResourcesMapper roleResourcesMapper;
	@Resource
	private RoleProductMapper roleProductMapper;
	@Resource
	private RoleMenuMapper roleMenuMapper;

	@Override
	public List<Role> queryRoleListWithSelected(Integer uid) {
		return roleMapper.queryRoleListWithSelected(uid);
	}

	@Override
	public PageInfo<Role> selectByPage(Role role, int start, int length) {
		int page = start / length + 1;
		Example example = new Example(Role.class);
		// 分页查询
		PageHelper.startPage(page, length);
		List<Role> rolesList = selectByExample(example);
		return new PageInfo<>(rolesList);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public void delRole(Integer roleid) {
		// 删除角色
		mapper.deleteByPrimaryKey(roleid);
		// 删除角色资源
		Example example = new Example(RoleResources.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("roleid", roleid);
		roleResourcesMapper.deleteByExample(example);

		// 删除主菜单
		Example example3 = new Example(RoleProduct.class);
		Example.Criteria criteria3 = example3.createCriteria();
		criteria3.andEqualTo("roleid", roleid);
		roleProductMapper.deleteByExample(example3);
		// 删除子菜单
		Example example4 = new Example(RoleMenu.class);
		Example.Criteria criteria4 = example4.createCriteria();
		criteria4.andEqualTo("roleid", roleid);
		roleMenuMapper.deleteByExample(example4);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public List<Role> queryAllRole() {
		return roleMapper.queryAllRole();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public int save(Role entity) {
		roleMapper.insertSelective(entity);
		int id = entity.getId();
		RoleProduct roleProduct = new RoleProduct();
		roleProduct.setRoleid(id);
		roleProduct.setProductid(entity.getProductIds());
		roleProductMapper.insertSelective(roleProduct);

		RoleMenu roleMenu = new RoleMenu();
		roleMenu.setRoleid(id);
		roleMenu.setMenuid(entity.getMenuIds());
		roleMenuMapper.insertSelective(roleMenu);
		return id;
	}

	@Override
	public int updateNotNull(Role entity) {
		roleMapper.updateByPrimaryKey(entity);
		int id = entity.getId();
		RoleProduct roleProduct = new RoleProduct();
		roleProduct.setRoleid(id);
		roleProduct.setProductid(entity.getProductIds());

		RoleMenu roleMenu = new RoleMenu();
		roleMenu.setRoleid(id);
		roleMenu.setMenuid(entity.getMenuIds());

		Example example = new Example(UserRole.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("roleid", id);

		roleProductMapper.updateByExampleSelective(roleProduct, example);
		roleMenuMapper.updateByExampleSelective(roleMenu, example);
		return id;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public void delRoles(List<Integer> ids) {
		Example example = new Example(Role.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("id", ids);
		mapper.deleteByExample(example);

		// 删除角色资源
		Example example2 = new Example(RoleResources.class);
		Example.Criteria criteria2 = example2.createCriteria();
		criteria2.andIn("roleid", ids);
		roleResourcesMapper.deleteByExample(example2);

		// 删除主菜单
		Example example3 = new Example(RoleProduct.class);
		Example.Criteria criteria3 = example3.createCriteria();
		criteria3.andIn("roleid", ids);
		roleProductMapper.deleteByExample(example3);
		// 删除子菜单
		Example example4 = new Example(RoleMenu.class);
		Example.Criteria criteria4 = example4.createCriteria();
		criteria4.andIn("roleid", ids);
		roleMenuMapper.deleteByExample(example4);
	}

	@Override
	public Role selectByRolename(String username) {
		Example example = new Example(Role.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("roledesc", username);

		List<Role> roleList = selectByExample(example);
		if (roleList.size() > 0) {
			return roleList.get(0);
		}
		return null;
	}
}
